package com.game.HandleData;

import java.util.concurrent.ConcurrentLinkedQueue;

public class Pack {
	private byte postion=0;
	private byte[]pack=null;
	private static ConcurrentLinkedQueue<Pack> packQueue=new ConcurrentLinkedQueue<Pack>();
	
	/**
	 * 回收pack对象
	 * @param pack
	 */
	public void PutPack(Pack pack){
		pack.setPack(null);
		packQueue.add(pack);
	}
	
	/**
	 * 获取pack对象
	 * @return Pack
	 */
	public static Pack getPackObj(){
		if(!packQueue.isEmpty())
			return packQueue.poll();
		return new Pack();
	}
	
	/**
	 * 获取指定位置的值
	 * @param pack
	 * @param pos
	 * @return
	 */
	public int get(byte[]pack,int pos){
		return pack[pos];
	} 
	
	/**
	 * 获取pack对象的下一个值
	 * postion自动指向下一个数值
	 * @return
	 */
	public byte get(){
		postion++;
		return this.pack[postion];
	}
	
	public int getPostion(){
		return postion;
	}
	  /**
     * byte[]某段元素转int  按照网络字节顺序
     * @param begin
     * @param end
     * @param Pack
     * @return
     */
	public  int ByteToInt(int begin ,int end, byte[] Pack){
		int mask  = 0xff;
		int temp  = 0;
		int res   = 0;
		for(int i=begin;i<=end;i++){
		    res<<=8;
		    temp  = Pack[i]&mask;
		    res  |= temp;
		}
		return res;
	}

	public ConcurrentLinkedQueue<Pack> getPackQueue() {
		return packQueue;
	}
	
	public byte[] getPack() {
		return pack;
	}

	public void setPack(byte[] pack) {
		this.pack = pack;
	}
}
